<?php
declare (strict_types=1);

namespace app\common\service;

use app\common\enum\file\StorageEnum;
use app\common\enum\SettingEnum;
use think\facade\Cache;
use app\common\enum\sms\SceneEnum;
use app\common\model\SettingModel;

class SettingService extends BaseService
{
    protected $cacheTag = 'system_setting';

    /**
     * 获取指定项设置
     */
    public static function getItem(string $key)
    {
        $data = self::getAll();
        return isset($data[$key]) ? $data[$key]['value_data'] : [];
    }

    /**
     * 全局缓存: 系统设置
     */
    public static function getAll(): array
    {
        if (!$data = Cache::get((new self())->cacheTag)) {
            // 获取设置列表
            // $setting = SettingModel::select();
            // $data = $setting->isEmpty() ? [] : array_column($setting->toArray(), 'key');
            $data = [];

            // 写入缓存中
            Cache::tag('cache')->set("system_setting", $data);
        }

        // 合并默认设置
        return array_merge_multiple(self::defaultData(), $data);
    }

    /**
     * 默认配置
     * @return array
     */
    public static function defaultData(): array
    {
        return [
            // 上传设置
            SettingEnum::STORAGE => [
                'key' => SettingEnum::STORAGE,
                'describe' => '上传设置',
                'value_data' => [
                    'default' => StorageEnum::LOCAL,
                    'engine' => [
                        StorageEnum::LOCAL => null,
                        StorageEnum::QINIU => [
                            'bucket' => '',
                            'access_key' => '',
                            'secret_key' => '',
                            'domain' => 'http://'
                        ],
                        StorageEnum::ALIYUN => [
                            'bucket' => '',
                            'access_key_id' => '',
                            'access_key_secret' => '',
                            'domain' => 'http://'
                        ],
                        StorageEnum::QCLOUD => [
                            'bucket' => '',
                            'region' => '',
                            'secret_id' => '',
                            'secret_key' => '',
                            'domain' => 'http://'
                        ],
                    ]
                ],
            ],
            // 短信通知
            SettingEnum::SMS => [
                'key' => SettingEnum::SMS,
                'describe' => '短信通知',
                'value_data' => [
                    'default' => 'chuanglan',
                    // 短信服务渠道
                    'gateways' => [
                        // 阿里云
                        'aliyun' => [
                            'access_key_id' => '',
                            'access_key_secret' => '',
                            'sign_name' => '',
                        ],
                        // 253云通讯（创蓝）
                        'chuanglan' => [
                            'account' => '',
                            'password' => '',
                            // 国际短信时必填
                            'intel_account' => '',
                            'intel_password' => '',
                            'channel'  => \Overtrue\EasySms\Gateways\ChuanglanGateway::CHANNEL_VALIDATE_CODE,
                            // 会员营销通道 特定参数。创蓝规定：api提交营销短信的时候，需要自己加短信的签名及退订信息
                            'sign' => '【通讯云】',
                            'unsubscribe' => '回TD退订',
                        ],
                    ],
                    // 短信通知场景
                    'scene' => [
                        // 登录
                        SceneEnum::LOGIN => [
                            'name' => '短信验证码 (通知用户)',    // 场景名称
                            'is_enable' => true,     // 是否开启
                            'template' => '',    // 模板ID
                            'content' => '您的验证码为：${code}，请勿泄露于他人！',
                            'variable' => ['code']                        ],
                    ]
                ],
            ],
        ];
    }

    public static function update(string $key, array $values)
    {
        $model = SettingModel::where('key', $key)->find() ?: new SettingModel;
        $model->save([
            'key' => $key,
            'describe' => SettingEnum::data()[$key]['describe'],
            'value_data' => $values,
        ]);
        Cache::delete((new self())->cacheTag);
        return render_array_success();
    }
}
